//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	IRulesService.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System.Collections.Generic;

namespace Smart.Common.Rules
{
	/// <summary>
	/// Rules service
	/// </summary>
	public interface IRulesService
	{
		/// <summary>
		/// Current context
		/// </summary>
		RulesCtx Ctx { get; set; }

		/// <summary>
		/// Rules source
		/// </summary>
		RulesSource Source { get; set; }

		/// <summary>
		/// Rule checker
		/// </summary>
		RuleChecker Checker { get; set; }

		/// <summary>
		/// Rule executor
		/// </summary>
		RuleExecutor Executor { get; set; }

		/// <summary>
		/// Rules service initialization
		/// </summary>
		void InitService();

		/// <summary>
		/// Create source method
		/// </summary>
		/// <returns>Rule source</returns>
		RulesSource CreateSource();

		/// <summary>
		/// Create checker method
		/// </summary>
		/// <returns>Rule checker</returns>
		RuleChecker CreateChecker();

		/// <summary>
		/// Create executor method
		/// </summary>
		/// <returns>Rule executor</returns>
		RuleExecutor CreateExecutor();

		/// <summary>
		/// Create context method
		/// </summary>
		/// <param name="rulesServiceName"></param>
		/// <returns>Rule context</returns>
		void InitContext(string rulesServiceName);

		/// <summary>
		/// Load instance
		/// </summary>
		IRulesInstance LoadInstance(string instanceId);

		/// <summary>
		/// Execute rules for instance
		/// </summary>
		/// <param name="instanceId">Instance identifier</param>
		/// <returns>true, if some rules' actions applied</returns>
		bool Execute(string instanceId);

		/// <summary>
		/// Execute rules for instance
		/// </summary>
		/// <returns>true, if some rules' actions applied</returns>
		bool Execute<TInstanceType>(TInstanceType instance)
			where TInstanceType : class, IRulesInstance;

		/// <summary>
		/// Execute rule
		/// </summary>
		/// <returns>true, if condition is fulfilled and action applied</returns>
		bool Execute<TRuleType, TInstanceType>(TRuleType rule, TInstanceType instance, IDictionary<string, object> variables)
			where TRuleType : class, IRule
			where TInstanceType : class, IRulesInstance;

		/// <summary>
		/// Execute rule
		/// </summary>
		/// <returns>true, if condition is fulfilled and action applied</returns>
		bool Execute<TInstanceType>(object rule, TInstanceType instance, IDictionary<string, object> variables)
			where TInstanceType : class, IRulesInstance;

		/// <summary>
		/// Check rule's condition
		/// </summary>
		/// <returns>true, if condition is fulfilled</returns>
		bool Check<TRuleConditionType, TInstanceType>(TRuleConditionType ruleCondition, TInstanceType instance, IDictionary<string, object> variables)
			where TRuleConditionType : class, IRuleCondition
			where TInstanceType : class, IRulesInstance;

		/// <summary>
		/// Check rule's condition
		/// </summary>
		/// <returns>true, if condition is fulfilled</returns>
		bool Check<TInstanceType>(object ruleCondition, TInstanceType instance, IDictionary<string, object> variables)
			where TInstanceType : class, IRulesInstance;

		/// <summary>
		/// Apply rule's action for instance
		/// </summary>
		bool Apply<TRuleActionType, TInstanceType>(TRuleActionType ruleAction, TInstanceType instance, IDictionary<string, object> variables)
			where TRuleActionType : class, IRuleAction
			where TInstanceType : class, IRulesInstance;

		/// <summary>
		/// Apply rule's action for instance
		/// </summary>
		bool Apply<TInstanceType>(object ruleAction, TInstanceType instance, IDictionary<string, object> variables)
			where TInstanceType : class, IRulesInstance;

		/// <summary>
		/// Load instance variables in context
		/// </summary>
		void LoadInstanceVariables(IRulesInstance instance);

		/// <summary>
		/// Create new securable filter
		/// </summary>
		RulesFilter CreateFilter();

		/// <summary>
		/// Load instance
		/// </summary>
		IRulesInstance LoadInstance(RulesFilter filter);
	}
}